package com.tanger.common.helper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description TODO
 * @Author Hchenbin
 * @Date 2022/5/9 10:54
 * @Version 1.0
 */
public class BatchHelper {

    /**
     * 批次处理集合，最大值为800
     */
    public final static int BATCH_MAX_COUNT = 800;

    /**
     *
     * 将集合按BATCH_MAX_COUNT批次分类
     *
     * @param list
     * @return
     */
    public static <T> Map<Integer, List<T>> collectionAndBatchProcessing(List<T> list){

        Map<Integer, List<T>> result = new HashMap<>();

        List<T> l = new ArrayList<>();
        int batchIndex = 1;

        for(int k = 0; k < list.size(); k++){
            if(k > 0 && k % BATCH_MAX_COUNT == 0){
                result.put(batchIndex, l);
                batchIndex++;
                l = new ArrayList<>();
            }
            l.add(list.get(k));
        }

        if(l.size() > 0){
            result.put(batchIndex, l);
        }

        return result;
    }


    public static void main(String[] args) {

        List<String> l = new ArrayList<>();

        for(int i = 0; i <= 1601; i++){
            l.add("test-"+i);
        }
        Map<Integer, List<String>> result = BatchHelper.collectionAndBatchProcessing(l);
        System.out.println(result.size());
        System.out.println("123");
    }
}
